/* eslint-disable import/prefer-default-export */
import CryptoJS from 'crypto-js';
import { TreeNodeData } from '@arco-design/web-vue';
import { Menu } from '@/store/modules/menu/types';
/**
 * 加密
 *
 * @param params 参数
 * @returns
 */
export function encryption(params: { key: string; password: string }) {
  const { key, password } = params;
  const iv = CryptoJS.enc.Utf8.parse(key);
  // 加密
  const encrypted = CryptoJS.AES.encrypt(password, iv, {
    iv,
    mode: CryptoJS.mode.CBC,
    padding: CryptoJS.pad.ZeroPadding,
  });
  return encrypted.toString();
}
/**
 * 递归处理
 * 树形菜单
 * []
 */
export const assembleTree = (menuList: Menu[]): TreeNodeData[] => {
  return menuList?.map((item) => {
    const { id, title } = item;
    // ??为NULL判断运算符，运算符左侧的值为null或undefined时，才会返回右侧的值
    const children = item.children ?? [];
    const isChild = children != null && children.length !== 0;
    const currentNode: TreeNodeData = {
      key: id,
      title,
      children: [],
    };
    // 是否有孩子节点，并递归处理
    if (isChild) {
      currentNode.children = assembleTree(children);
    }
    return currentNode;
  });
};
